package com.bkq.cb;



import java.text.SimpleDateFormat;
import java.util.Date;

import com.bkq.cb.BLO.LoanBLO;
import com.bkq.cb.BLO.TransactionBLO;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;

public class DayReportScreen extends TabActivity implements TabHost.TabContentFactory {
	private TextView today,title;
	private SqliteServices service = null;
	private Cursor mCursor;
	Button back,list,search;
	GetDMY g = new GetDMY();
	Date date;
	 TransactionBLO t = new TransactionBLO(this);
	 LoanBLO l = new LoanBLO(this);
	String idUser,nameUser;
	 SimpleDateFormat dayForm;
	 TextView moneyIn,moneyEx,moneyBaTra,moneyDeb,moneyLoa,moneyBaLoa;
	 TextView moneyIn_W,moneyEx_W,moneyBaTra_W,moneyDeb_W,moneyLoa_W,moneyBaLoa_W;
	 TextView moneyIn_M,moneyEx_M,moneyBaTra_M,moneyDeb_M,moneyLoa_M,moneyBaLoa_M;
	public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
	     setContentView(R.layout.reportscreen);
	     service = new SqliteServices(this);
	     today = (TextView)findViewById(R.id.todayTv_Report); 
	   //  
	     title = (TextView)findViewById(R.id.title_Report); 
	     back = (Button)findViewById(R.id.backIma_Report);
	  list = (Button)findViewById(R.id.listIma_Report);
	 search = (Button)findViewById(R.id.searchIma_Report);
	 search.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(DayReportScreen.this,SearchScreen.class));
			}
		});
	     back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	     
	     Bundle extras = getIntent().getExtras();
		    if (extras != null) 
		    {
		    	idUser = extras.getString("idUser").toString();
		    	nameUser = extras.getString("nameUser").toString();
		    }
	      dayForm = new SimpleDateFormat("d/M/yyyy");
	        date = new Date();
	       // moneyIn.setText(idUser);
	     TabHost tabHost = getTabHost();
	     getTabHost().setCurrentTab(0);
	  today.setText("Select Tab");
	     list.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent i = new Intent(DayReportScreen.this,SelectModeReport.class);
					i.putExtra("idUser", idUser);
					i.putExtra("nameUser", nameUser);
					startActivity(i);
				}
			});
			
     //    today.setText(String.valueOf(dayForm.format(date).toString()));

			tabHost.addTab(tabHost.newTabSpec("day").setIndicator("Day").setContent(this));
			tabHost.addTab(tabHost.newTabSpec("week").setIndicator("Week").setContent(this));
			tabHost.addTab(tabHost.newTabSpec("month").setIndicator("Month").setContent(this));
			setupUI();
	 }

	@Override
	public View createTabContent(String tag) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = getLayoutInflater();
		View convertView = null;
		if (tag.equals("day")) {
			convertView=inflater.inflate(R.layout.day, null, true);
		} else if (tag.equals("week")) {
			convertView=inflater.inflate(R.layout.week, null, false);
		}else if (tag.equals("month")) {
				convertView=inflater.inflate(R.layout.month, null, false);
			
		}return convertView;	
	}
	
	private void setupUI() {
		RadioButton rbFirst = (RadioButton) findViewById(R.id.first);
		
		RadioButton rbSecond = (RadioButton) findViewById(R.id.second); 
		
		RadioButton rbThird = (RadioButton) findViewById(R.id.third); 
		//rbFirst.setText("Day");
		rbFirst.setButtonDrawable(R.drawable.dayicon);
		//rbSecond.setText("Week");
		rbSecond.setButtonDrawable(R.drawable.weekicon);
		//rbThird.setText("Month");
		rbThird.setButtonDrawable(R.drawable.monthicon);
	
		
		RadioGroup rg = (RadioGroup) findViewById(R.id.states);
		rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, final int checkedId) {
				 float s=0,s1=0,s2=0,s3=0;
				
				switch (checkedId) {
				case R.id.first:
					getTabHost().setCurrentTab(0);
					moneyIn = (TextView)findViewById(R.id.moneyIncome_Day);
					moneyEx = (TextView)findViewById(R.id.moneyExpense_Day);
					moneyBaTra = (TextView)findViewById(R.id.moneyBalanceTran_Day);
					moneyLoa = (TextView)findViewById(R.id.moneyLoan_Day);
					moneyDeb = (TextView)findViewById(R.id.moneyDebt_Day);
					moneyBaLoa = (TextView)findViewById(R.id.moneyBalanceLoan_Day);
					 title.setText("Day Report");
				         today.setText(String.valueOf(dayForm.format(date).toString()));
				     mCursor = service.queryData("Select * from tblTransaction");
				    
				     if(mCursor.moveToFirst())
				     {
				    	
						if(mCursor.getString(3).equals(today.getText().toString())&&mCursor.getString(1).equals(idUser))
						{
							if(mCursor.getString(5).equals("income"))
							s+=Float.parseFloat(mCursor.getString(4).toString());
							else s1+=Float.parseFloat(mCursor.getString(4).toString());
						}
				    	 while(mCursor.moveToNext())
				    	 {
				    		 if(mCursor.getString(3).equals(today.getText().toString())&&mCursor.getString(1).equals(idUser) )
								{
				    			 if(mCursor.getString(5).equals("income"))
										s+=Float.parseFloat(mCursor.getString(4).toString());
										else s1+=Float.parseFloat(mCursor.getString(4).toString());
								}
				    	 }
				    	
				     }
				     
				moneyIn.setText(String.valueOf(s));
				moneyEx.setText(String.valueOf(s1));
				moneyBaTra.setText(String.valueOf(s-s1));
				 mCursor = service.queryData("Select * from tblLoan");
			     if(mCursor.moveToFirst())
			     {
			    	
					if(mCursor.getString(3).equals(today.getText().toString())&&mCursor.getString(1).equals(idUser))
					{
						if(mCursor.getString(6).equals("Lend"))
						s2+=Float.parseFloat(mCursor.getString(5).toString());
						else s3+=Float.parseFloat(mCursor.getString(5).toString());
					}
			    	 while(mCursor.moveToNext())
			    	 {
			    		 if(mCursor.getString(3).equals(today.getText().toString())&&mCursor.getString(1).equals(idUser) )
							{
			    			 if(mCursor.getString(6).equals("Lend"))
									s2+=Float.parseFloat(mCursor.getString(5).toString());
									else s3+=Float.parseFloat(mCursor.getString(5).toString());
							}
			    	 }
			    	
			     }
			     moneyLoa.setText(String.valueOf(s2));
					moneyDeb.setText(String.valueOf(s3));
					moneyBaLoa.setText(String.valueOf(s2-s3));
					mCursor.close();
					
					break;
				case R.id.second:
					getTabHost().setCurrentTab(1);
			         today.setText(String.valueOf(dayForm.format(date).toString()));
					 title.setText("Week Report");
					 String m,y;
					 m = g.getMonth(today.getText().toString());
					 y = g.getYear(today.getText().toString());
				//	today.setText("From - To");
					 moneyIn_W = (TextView)findViewById(R.id.moneyIncome_Week);
						moneyEx_W = (TextView)findViewById(R.id.moneyExpense_Week);
						moneyBaTra_W = (TextView)findViewById(R.id.moneyBalanceTran_Week);
						moneyLoa_W = (TextView)findViewById(R.id.moneyLoan_Week);
						moneyDeb_W = (TextView)findViewById(R.id.moneyDebt_Week);
					moneyBaLoa_W = (TextView)findViewById(R.id.moneyBalanceLoan_Week);
					 float[] k2 = new float[8];
					 k2 =  l.getValue(today.getText().toString(), idUser);  
					  moneyIn_W.setText(String.valueOf(k2[0]));
					moneyEx_W.setText(String.valueOf(k2[1]));
					moneyBaTra_W.setText(String.valueOf(k2[2]));

					 moneyLoa_W.setText(String.valueOf(k2[3]));
					moneyDeb_W.setText(String.valueOf(k2[4]));
					moneyBaLoa_W.setText(String.valueOf(k2[5]));
					today.setText(String.valueOf((int)k2[6])+"/"+m+"/"+y+" - "+
							String.valueOf((int)k2[7])+"/"+m+"/"+y);
					break;
				case R.id.third:
					getTabHost().setCurrentTab(2);
					 title.setText("Month Report");
			         today.setText(String.valueOf(dayForm.format(date).toString()));

					 moneyIn_M = (TextView)findViewById(R.id.moneyIncome_Month);
						moneyEx_M = (TextView)findViewById(R.id.moneyExpense_Month);
						moneyBaTra_M = (TextView)findViewById(R.id.moneyBalanceTran_Month);
						moneyLoa_M = (TextView)findViewById(R.id.moneyLoan_Month);
						moneyDeb_M = (TextView)findViewById(R.id.moneyDebt_Month);
					moneyBaLoa_M = (TextView)findViewById(R.id.moneyBalanceLoan_Month);
					
					//today.setText("Month...");
					 float[] k = new float[6];
					 k =  t.getValue(today.getText().toString(), idUser);  
					  moneyIn_M.setText(String.valueOf(k[0]));
					moneyEx_M.setText(String.valueOf(k[1]));
					moneyBaTra_M.setText(String.valueOf(k[2]));
					today.setText(String.valueOf(Integer.parseInt(g.getMonth(today.getText().toString()))-1)+"/"+g.getYear(today.getText().toString()));
					// float[] k1 = new float[3];
					
				 
					 moneyLoa_M.setText(String.valueOf(k[3]));
					moneyDeb_M.setText(String.valueOf(k[4]));
					moneyBaLoa_M.setText(String.valueOf(k[5]));
				
					break;
								}
			}
		});
	}
}
